//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "YoYOptionletHelper.h"
using namespace Cephei::QL::Experimental::Inflation;
#include <gen/QL/Termstructures/Volatility/Inflation/YoYOptionletVolatilitySurface.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Indexes/YoYInflationIndex.h>
#include <gen/QL/Pricingengines/Inflation/YoYInflationCapFloorEngine.h>
using namespace Cephei::QL::Termstructures::Volatility::Inflation;
using namespace Cephei::QL;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Pricingengines::Inflation;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Inflation::CYoYOptionletHelper::CYoYOptionletHelper (Cephei::QL::IQuote^ price, Double notional, QL::Instruments::YoYInflationCapFloor::TypeEnum capFloorType, Cephei::QL::Times::IPeriod^ lag, Cephei::QL::Times::IDayCounter^ yoyDayCounter, Cephei::QL::Times::ICalendar^ paymentCalendar, UInt32 fixingDays, Cephei::QL::Indexes::IYoYInflationIndex^ index, Double strike, UInt64 n, Cephei::QL::Pricingengines::Inflation::IYoYInflationCapFloorEngine^ pricer) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CQuote^ _Cprice;
    CPeriod^ _Clag;
    CDayCounter^ _CyoyDayCounter;
    CCalendar^ _CpaymentCalendar;
    CYoYInflationIndex^ _Cindex;
    CYoYInflationCapFloorEngine^ _Cpricer;
    try
    {
#ifdef HANDLE
        _phYoYOptionletHelper = NULL;
#endif
        _Cprice = safe_cast<CQuote^> (price);
        _Cprice->Lock();
        Handle<QuantLib::Quote>& _price = static_cast<Handle<QuantLib::Quote>&> (_Cprice->GetHandle ()); 
        QuantLib::Real _notional = (QuantLib::Real)ValueHelper::Convert (notional); //d
        QuantLib::YoYInflationCapFloor::Type _capFloorType = (QuantLib::YoYInflationCapFloor::Type)capFloorType ;
        _Clag = safe_cast<CPeriod^> (lag);
        _Clag->Lock();
        QuantLib::Period& _lag = static_cast<QuantLib::Period&> (_Clag->GetReference ()); 
        _CyoyDayCounter = safe_cast<CDayCounter^> (yoyDayCounter);
        _CyoyDayCounter->Lock();
        QuantLib::DayCounter& _yoyDayCounter = static_cast<QuantLib::DayCounter&> (_CyoyDayCounter->GetReference ()); 
        _CpaymentCalendar = safe_cast<CCalendar^> (paymentCalendar);
        _CpaymentCalendar->Lock();
        QuantLib::Calendar& _paymentCalendar = static_cast<QuantLib::Calendar&> (_CpaymentCalendar->GetReference ()); 
        QuantLib::Natural _fixingDays = (QuantLib::Natural)ValueHelper::Convert (fixingDays); //d
        _Cindex = safe_cast<CYoYInflationIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::YoYInflationIndex>& _index = static_cast<boost::shared_ptr<QuantLib::YoYInflationIndex>&> (_Cindex->GetShared ()); 
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike); //d
        QuantLib::Size _n = (QuantLib::Size)ValueHelper::Convert (n); //d
        _Cpricer = safe_cast<CYoYInflationCapFloorEngine^> (pricer);
        _Cpricer->Lock();
        boost::shared_ptr<QuantLib::YoYInflationCapFloorEngine>& _pricer = static_cast<boost::shared_ptr<QuantLib::YoYInflationCapFloorEngine>&> (_Cpricer->GetShared ()); 
        _ppYoYOptionletHelper = new boost::shared_ptr<QuantLib::YoYOptionletHelper> (new QuantLib::YoYOptionletHelper ( _price,  _notional,  _capFloorType,  _lag,  _yoyDayCounter,  _paymentCalendar,  _fixingDays,  _index,  _strike,  _n,  _pricer ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprice != nullptr) _Cprice->Unlock();
        if (_Clag != nullptr) _Clag->Unlock();
        if (_CyoyDayCounter != nullptr) _CyoyDayCounter->Unlock();
        if (_CpaymentCalendar != nullptr) _CpaymentCalendar->Unlock();
        if (_Cindex != nullptr) _Cindex->Unlock();
        if (_Cpricer != nullptr) _Cpricer->Unlock();
    }
}
Cephei::QL::Experimental::Inflation::CYoYOptionletHelper::CYoYOptionletHelper (boost::shared_ptr<QuantLib::YoYOptionletHelper>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYOptionletHelper = NULL;
#endif
	_ppYoYOptionletHelper = &childNative;
    
}
Cephei::QL::Experimental::Inflation::CYoYOptionletHelper::CYoYOptionletHelper (QuantLib::YoYOptionletHelper& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phYoYOptionletHelper = NULL;
#endif
	_ppYoYOptionletHelper = new boost::shared_ptr<QuantLib::YoYOptionletHelper> (&childNative);
    
    _YoYOptionletHelperOwner = owner;
    
}

Cephei::QL::Experimental::Inflation::CYoYOptionletHelper::CYoYOptionletHelper (CYoYOptionletHelper^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYOptionletHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppYoYOptionletHelper = new boost::shared_ptr<QuantLib::YoYOptionletHelper> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Inflation::CYoYOptionletHelper::CYoYOptionletHelper (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYOptionletHelper = NULL;
#endif
	if (!t->IsSubclassOf(CYoYOptionletHelper::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Inflation::CYoYOptionletHelper::CYoYOptionletHelper (QuantLib::Handle<QuantLib::YoYOptionletHelper>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phYoYOptionletHelper = &childNative;
	_ppYoYOptionletHelper = &static_cast<boost::shared_ptr<QuantLib::YoYOptionletHelper>>(childNative.currentLink());
    
    _YoYOptionletHelperOwner = owner;
}
Cephei::QL::Experimental::Inflation::CYoYOptionletHelper::CYoYOptionletHelper (QuantLib::Handle<QuantLib::YoYOptionletHelper> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phYoYOptionletHelper = &childNative;
	_ppYoYOptionletHelper = &static_cast<boost::shared_ptr<QuantLib::YoYOptionletHelper>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Inflation::CYoYOptionletHelper::CYoYOptionletHelper (QuantLib::YoYOptionletHelper childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYOptionletHelper = NULL;
#endif
	_ppYoYOptionletHelper = new boost::shared_ptr<QuantLib::YoYOptionletHelper> (new QuantLib::YoYOptionletHelper (childNative));
    
}
#endif

Cephei::QL::Experimental::Inflation::CYoYOptionletHelper::~CYoYOptionletHelper ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppYoYOptionletHelper != NULL)
    {
	    delete _ppYoYOptionletHelper;
        _ppYoYOptionletHelper = NULL;
    }
}
Cephei::QL::Experimental::Inflation::CYoYOptionletHelper::!CYoYOptionletHelper ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppYoYOptionletHelper != NULL)
    {
	    delete _ppYoYOptionletHelper;
    }
}
QuantLib::YoYOptionletHelper& Cephei::QL::Experimental::Inflation::CYoYOptionletHelper::GetReference ()
{
    if (_ppYoYOptionletHelper == NULL) throw REFNEW NativeNullException ();
	return **_ppYoYOptionletHelper;
}
boost::shared_ptr<QuantLib::YoYOptionletHelper>& Cephei::QL::Experimental::Inflation::CYoYOptionletHelper::GetShared ()
{
    if (_ppYoYOptionletHelper == NULL) throw REFNEW NativeNullException ();
	return *_ppYoYOptionletHelper;
}
QuantLib::YoYOptionletHelper* Cephei::QL::Experimental::Inflation::CYoYOptionletHelper::GetPointer ()
{
    if (_ppYoYOptionletHelper == NULL) throw REFNEW NativeNullException ();
	return &**_ppYoYOptionletHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::YoYOptionletHelper>& Cephei::QL::Experimental::Inflation::CYoYOptionletHelper::GetHandle ()
{
	if (_phYoYOptionletHelper == NULL)
	{
		_phYoYOptionletHelper = new Handle<QuantLib::YoYOptionletHelper> (*_ppYoYOptionletHelper);
	}
	return *_phYoYOptionletHelper;
}
#endif
bool Cephei::QL::Experimental::Inflation::CYoYOptionletHelper::HasNative () 
{
	return (_ppYoYOptionletHelper != NULL);
}

Double Cephei::QL::Experimental::Inflation::CYoYOptionletHelper::ImpliedQuote::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppYoYOptionletHelper)->impliedQuote ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Inflation::IYoYOptionletHelper^ Cephei::QL::Experimental::Inflation::CYoYOptionletHelper::SetTermStructure (Cephei::QL::Termstructures::Volatility::Inflation::IYoYOptionletVolatilitySurface^ v)
{
    CYoYOptionletVolatilitySurface^ _Cv;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cv = safe_cast<CYoYOptionletVolatilitySurface^> (v);
        _Cv->Lock();
        QuantLib::YoYOptionletVolatilitySurface* _v = static_cast<QuantLib::YoYOptionletVolatilitySurface*> (_Cv->GetPointer ()); 
    	(*_ppYoYOptionletHelper)->setTermStructure ( _v );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cv != nullptr) _Cv->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Inflation::IYoYOptionletHelper^ Cephei::QL::Experimental::Inflation::CYoYOptionletHelper_Factory::Create (Cephei::QL::IQuote^ price, Double notional, QL::Instruments::YoYInflationCapFloor::TypeEnum capFloorType, Cephei::QL::Times::IPeriod^ lag, Cephei::QL::Times::IDayCounter^ yoyDayCounter, Cephei::QL::Times::ICalendar^ paymentCalendar, UInt32 fixingDays, Cephei::QL::Indexes::IYoYInflationIndex^ index, Double strike, UInt64 n, Cephei::QL::Pricingengines::Inflation::IYoYInflationCapFloorEngine^ pricer)
{
    return REFNEW CYoYOptionletHelper ( price,  notional,  capFloorType,  lag,  yoyDayCounter,  paymentCalendar,  fixingDays,  index,  strike,  n,  pricer);
}
